package com.want.evenchat.controller.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Window;
import android.widget.RadioGroup;
import com.want.evenchat.R;
import com.want.evenchat.controller.fragment.ChatFragment;
import com.want.evenchat.controller.fragment.ContactListFragment;
import com.want.evenchat.controller.fragment.SettingFragment;

public class MainActivity extends FragmentActivity {

    private RadioGroup radioGroup;
    private ChatFragment chatFragment;
    private ContactListFragment contactListFragment;
    private SettingFragment settingFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        initView();

        initData();

        initListener();
    }

    private void initView(){
        radioGroup = findViewById(R.id.radiogroup);
    }

    private void initData(){
        //创建三个fragment对象
        chatFragment = new ChatFragment();
        contactListFragment = new ContactListFragment();
        settingFragment = new SettingFragment();
    }

    private void initListener(){
        //RadioGroup的选择事件
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                Fragment fragment = null;
                switch (checkedId){
                    //会话列表页面
                    case R.id.radiobutton_chat:
                        fragment = chatFragment;
                        break;
                    //联系人列表页面
                    case R.id.radiobutton_contact:
                        fragment = contactListFragment;
                        break;
                    //设置页面
                    case R.id.radiobutton_setting:
                        fragment = settingFragment;
                        break;
                }
                //实现Fragment切换的方法
                switchFragment(fragment);
            }
        });

        //默认选择一个Fragment
        radioGroup.check(R.id.radiobutton_chat);
    }

    private void switchFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.fragment_main , fragment).commit();
    }
}
